package com.hejia.bulbul.controller;

import com.alibaba.fastjson.JSONObject;
import com.hejia.alauda.entity.Parameter;
import com.hejia.alauda.service.ConfigureService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * app底部导航图标
 *
 * managment - com.hejia.bulbul.controller
 * Created by clt on 15/1/16.下午3:17
 */
@Controller
@RequestMapping("navigation")
public class MobileNavigationController extends BaseController{

    @Resource
    private ConfigureService parameterService;

    @RequestMapping()
    public String navigation(Model model){
        Parameter android = parameterService.getParameter(Parameter.Category.Navigation_ICON, "Android");
        Parameter ios = parameterService.getParameter(Parameter.Category.Navigation_ICON, "iOS");
        setValue2Model(android, ios, model);
        model.addAttribute("androidParameter", android);
        model.addAttribute("iosParameter", ios);
        return "app/navigation";
    }


    /**
     * 设置值
     */
    private void setValue2Model(Parameter android, Parameter ios, Model model) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String[] keys = {"startTime", "endTime", "name", "link"};
        /*
         * 对应的值，json格式
         */
        if (android != null){
            value2Model(android, model, format, keys, "android");
        }
        if (ios != null) {
            value2Model(ios, model, format, keys, "iOS");
        }

    }

    private void value2Model(Parameter parameter, Model model, SimpleDateFormat format, String[] keys, String os) {
        JSONObject jsonObject = JSONObject.parseObject(parameter.getValue());
        for (String key : keys) {
            String value;

            if (key.equals("startTime") || key.equals("endTime")){
                long longValue = jsonObject.getLongValue(key);

                Date date = new Date(longValue);
                value = format.format(date);
            }else{
                value = (String) jsonObject.get(key);
            }

            model.addAttribute(os + key, value);
        }
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String addNavigation(Parameter parameter, RedirectAttributes redirectAttributes){

        parameter.setCreateDate(new Date());
        parameter.setModifyDate(new Date());
        parameter.setMemo("导航图标");
        boolean flag;


        JSONObject jsonObject = JSONObject.parseObject(parameter.getValue());
        String startTime = (String)jsonObject.get("startTime");
        String endTime = (String)jsonObject.get("endTime");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            jsonObject.put("startTime", format.parse(startTime).getTime());
            jsonObject.put("endTime", format.parse(endTime).getTime());
            parameter.setValue(jsonObject.toJSONString());

        } catch (ParseException e) {
            e.printStackTrace();
        }


        if (parameter.getId() == null){
            flag = parameterService.saveParameter(parameter);
        } else {
            flag = parameterService.updateParameter(parameter);
        }
        if(flag){
            if (parameter.getCode().equalsIgnoreCase("iOS")){
                redirectAttributes.addFlashAttribute("msg", "iOS导航图标更新成功");
            }else{
                redirectAttributes.addFlashAttribute("msg", "android导航图标更新成功");
            }
            redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
        }else{
            redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
            redirectAttributes.addFlashAttribute("msg", "导航图标更新失败");
        }
        return "redirect:/navigation";
    }

}
